{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba49c2998ed32020",
   "metadata": {},
   "source": [
    "## Building a Dappier Real Time Search Agent\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-dappier/examples/dappier_real_time_search.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "This tutorial walks through using the LLM tools provided by [Dappier](https://dappier.com/) to allow LLMs to use Dappier's pre-trained, LLM ready RAG models and natural language APIs to ensure factual, up-to-date, responses from premium content providers across key verticals like News, Finance, Sports, Weather, and more.\n",
    "\n",
    "\n",
    "To get started, you will need an [OpenAI API key](https://platform.openai.com/account/api-keys) and a [Dappier API key](https://platform.dappier.com/profile/api-keys)\n",
    "\n",
    "We will import the relevant agents and tools and pass them our keys here:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11992469",
   "metadata": {},
   "source": [
    "## Installation\n",
    "\n",
    "First, install the `llama-index` and `llama-index-tools-dappier` packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: llama-index in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (0.12.25)\n",
      "Requirement already satisfied: llama-index-tools-dappier in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (0.3.0)\n",
      "Requirement already satisfied: llama-index-agent-openai<0.5.0,>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.4.6)\n",
      "Requirement already satisfied: llama-index-cli<0.5.0,>=0.4.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.4.1)\n",
      "Requirement already satisfied: llama-index-core<0.13.0,>=0.12.25 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.12.25)\n",
      "Requirement already satisfied: llama-index-embeddings-openai<0.4.0,>=0.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.3.1)\n",
      "Requirement already satisfied: llama-index-indices-managed-llama-cloud>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.6.9)\n",
      "Requirement already satisfied: llama-index-llms-openai<0.4.0,>=0.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.3.25)\n",
      "Requirement already satisfied: llama-index-multi-modal-llms-openai<0.5.0,>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.4.3)\n",
      "Requirement already satisfied: llama-index-program-openai<0.4.0,>=0.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.3.1)\n",
      "Requirement already satisfied: llama-index-question-gen-openai<0.4.0,>=0.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.3.0)\n",
      "Requirement already satisfied: llama-index-readers-file<0.5.0,>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.4.6)\n",
      "Requirement already satisfied: llama-index-readers-llama-parse>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (0.4.0)\n",
      "Requirement already satisfied: nltk>3.8.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index) (3.9.1)\n",
      "Requirement already satisfied: dappier<0.4.0,>=0.3.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-tools-dappier) (0.3.3)\n",
      "Requirement already satisfied: asyncio<4.0.0,>=3.4.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (3.4.3)\n",
      "Requirement already satisfied: httpx<0.29.0,>=0.23.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (0.28.1)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=1.9 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (2.10.6)\n",
      "Requirement already satisfied: openai>=1.14.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-agent-openai<0.5.0,>=0.4.0->llama-index) (1.66.5)\n",
      "Requirement already satisfied: PyYAML>=6.0.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (6.0.2)\n",
      "Requirement already satisfied: SQLAlchemy>=1.4.49 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.13.0,>=0.12.25->llama-index) (2.0.39)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (3.11.14)\n",
      "Requirement already satisfied: dataclasses-json in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (0.6.7)\n",
      "Requirement already satisfied: deprecated>=1.2.9.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (1.2.18)\n",
      "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (1.0.8)\n",
      "Requirement already satisfied: filetype<2.0.0,>=1.2.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (1.2.0)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (2025.3.0)\n",
      "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (1.6.0)\n",
      "Requirement already satisfied: networkx>=3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (3.2.1)\n",
      "Requirement already satisfied: numpy in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (2.0.2)\n",
      "Requirement already satisfied: pillow>=9.0.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (11.1.0)\n",
      "Requirement already satisfied: requests>=2.31.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (2.32.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.2.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (9.0.0)\n",
      "Requirement already satisfied: tiktoken>=0.3.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (0.9.0)\n",
      "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (4.67.1)\n",
      "Requirement already satisfied: typing-extensions>=4.5.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (4.12.2)\n",
      "Requirement already satisfied: typing-inspect>=0.8.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (0.9.0)\n",
      "Requirement already satisfied: wrapt in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-core<0.13.0,>=0.12.25->llama-index) (1.17.2)\n",
      "Requirement already satisfied: llama-cloud<0.2.0,>=0.1.13 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-indices-managed-llama-cloud>=0.4.0->llama-index) (0.1.14)\n",
      "Requirement already satisfied: beautifulsoup4<5.0.0,>=4.12.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (4.13.3)\n",
      "Requirement already satisfied: pandas in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (2.2.3)\n",
      "Requirement already satisfied: pypdf<6.0.0,>=5.1.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (5.4.0)\n",
      "Requirement already satisfied: striprtf<0.0.27,>=0.0.26 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (0.0.26)\n",
      "Requirement already satisfied: llama-parse>=0.5.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-index-readers-llama-parse>=0.4.0->llama-index) (0.6.4.post1)\n",
      "Requirement already satisfied: click in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from nltk>3.8.1->llama-index) (8.1.8)\n",
      "Requirement already satisfied: joblib in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from nltk>3.8.1->llama-index) (1.4.2)\n",
      "Requirement already satisfied: regex>=2021.8.3 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from nltk>3.8.1->llama-index) (2024.11.6)\n",
      "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (2.6.1)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (1.3.2)\n",
      "Requirement already satisfied: async-timeout<6.0,>=4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (5.0.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (25.3.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (1.5.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (6.2.0)\n",
      "Requirement already satisfied: propcache>=0.2.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (0.3.0)\n",
      "Requirement already satisfied: yarl<2.0,>=1.17.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.13.0,>=0.12.25->llama-index) (1.18.3)\n",
      "Requirement already satisfied: soupsieve>1.2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from beautifulsoup4<5.0.0,>=4.12.3->llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (2.6)\n",
      "Requirement already satisfied: anyio in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (4.9.0)\n",
      "Requirement already satisfied: certifi in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (2025.1.31)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (1.0.7)\n",
      "Requirement already satisfied: idna in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (3.10)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from httpcore==1.*->httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (0.14.0)\n",
      "Requirement already satisfied: llama-cloud-services>=0.6.4 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-parse>=0.5.0->llama-index-readers-llama-parse>=0.4.0->llama-index) (0.6.5)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from openai>=1.14.0->llama-index-agent-openai<0.5.0,>=0.4.0->llama-index) (1.9.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from openai>=1.14.0->llama-index-agent-openai<0.5.0,>=0.4.0->llama-index) (0.9.0)\n",
      "Requirement already satisfied: sniffio in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from openai>=1.14.0->llama-index-agent-openai<0.5.0,>=0.4.0->llama-index) (1.3.1)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from pydantic<3.0.0,>=1.9->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from pydantic<3.0.0,>=1.9->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (2.27.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from requests>=2.31.0->llama-index-core<0.13.0,>=0.12.25->llama-index) (3.4.1)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from requests>=2.31.0->llama-index-core<0.13.0,>=0.12.25->llama-index) (2.3.0)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.13.0,>=0.12.25->llama-index) (3.1.1)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from typing-inspect>=0.8.0->llama-index-core<0.13.0,>=0.12.25->llama-index) (1.0.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from dataclasses-json->llama-index-core<0.13.0,>=0.12.25->llama-index) (3.26.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from pandas->llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from pandas->llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (2025.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from pandas->llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (2025.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from anyio->httpx<0.29.0,>=0.23.0->dappier<0.4.0,>=0.3.3->llama-index-tools-dappier) (1.2.2)\n",
      "Requirement already satisfied: python-dotenv<2.0.0,>=1.0.1 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from llama-cloud-services>=0.6.4->llama-parse>=0.5.0->llama-index-readers-llama-parse>=0.4.0->llama-index) (1.0.1)\n",
      "Requirement already satisfied: packaging>=17.0 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core<0.13.0,>=0.12.25->llama-index) (24.2)\n",
      "Requirement already satisfied: six>=1.5 in /Users/amaan/Library/Caches/pypoetry/virtualenvs/llama-index-yhiCGw8g-py3.10/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas->llama-index-readers-file<0.5.0,>=0.4.0->llama-index) (1.17.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install llama-index llama-index-tools-dappier"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7f3a766",
   "metadata": {},
   "source": [
    "## Setup API keys\n",
    "\n",
    "You'll need to set up your API keys for OpenAI and Dappier."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59886322",
   "metadata": {},
   "source": [
    "You can go to [here](https://platform.openai.com/settings/organization/api-keys) to get API Key from Open AI."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5316e0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "# Prompt for the API key securely\n",
    "openai_api_key = getpass(\"Enter your API key: \")\n",
    "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40347147",
   "metadata": {},
   "source": [
    "You can go to [here](https://platform.dappier.com/profile/api-keys) to get API Key from Dappier with **free** credits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf4fe7bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prompt for the Dappier API key securely\n",
    "dappier_api_key = getpass(\"Enter your API key: \")\n",
    "os.environ[\"DAPPIER_API_KEY\"] = dappier_api_key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7f73566",
   "metadata": {},
   "source": [
    "## Setup Dappier Tool\n",
    "\n",
    "Initialize the Dappier Real-Time Search Tool, convert it to a tool list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b0877f51a351135",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search_real_time_data\n",
      "search_stock_market_data\n"
     ]
    }
   ],
   "source": [
    "from llama_index.tools.dappier import DappierRealTimeSearchToolSpec\n",
    "\n",
    "dappier_tool = DappierRealTimeSearchToolSpec()\n",
    "\n",
    "dappier_tool_list = dappier_tool.to_tool_list()\n",
    "for tool in dappier_tool_list:\n",
    "    print(tool.metadata.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10363da6",
   "metadata": {},
   "source": [
    "## Usage\n",
    "\n",
    "We've imported our OpenAI agent, set up the api key, and initialized our tool. Let's test out the tool before setting up our Agent.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "427a81ad",
   "metadata": {},
   "source": [
    "### Search Real Time Data\n",
    "\n",
    "Access real-time google web search results including the latest news, weather, travel, deals and more."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2d1e7865b0301f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today in New York, it's around 40°F (about 4°C). It's a bit chilly, so definitely bundle up if you're heading outside! 🧥❄️ Stay warm!\n"
     ]
    }
   ],
   "source": [
    "print(dappier_tool.search_real_time_data(\"How is the weather in New York today?\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b45fe5c5",
   "metadata": {},
   "source": [
    "### Search Stock Market Data\n",
    "\n",
    "Access real-time financial news, stock prices, and trades from polygon.io, with AI-powered insights and up-to-the-minute updates to keep you informed on all your financial interests.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd6d5e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here are the latest financial news articles related to Meta:\n",
      "\n",
      "1. **Title:** [Shutdown Averted, Treasury Yields Flirt With 5%, Strong Payrolls Add to Fed Tightening Risk: The Week In The Markets](https://www.benzinga.com/markets/equities/23/10/35132440/shutdown-averted-treasury-yields-flirt-with-5-strong-payrolls-add-to-fed-tightening-risk-the-wee)  \n",
      "   **Author:** Piero Cingari  \n",
      "   **Published on:** October 6, 2023  \n",
      "   **Description:** A week filled with significant events impacting the markets culminated in an unexpectedly strong September jobs report. The article discusses the implications of rising Treasury yields and strong employment growth on the market, including mentions of various tickers including META.  \n",
      "   ![Image](https://cdn.benzinga.com/files/images/story/2023/Wall_Street_subway_sign_1.jpeg?width=1200&height=800&fit=crop)\n",
      "\n",
      "2. **Title:** [Recession Alert! JPMorgan's Kolanovic Foresees 20% Market Downturn](https://www.benzinga.com/markets/equities/23/10/35126766/recession-alert-jpmorgans-kolanovic-foresees-20-market-downturn)  \n",
      "   **Author:** Nabaparna Bhattacharya  \n",
      "   **Published on:** October 6, 2023  \n",
      "   **Description:** JPMorgan’s chief market strategist Marko Kolanovic forecasts a potential 20% decline in the S&P 500 due to rising interest rates. The article suggests investors should consider cash and short-term Treasurys as a protective strategy, mentioning META among other tickers.  \n",
      "   ![Image](https://cdn.benzinga.com/files/images/story/2023/10/06/recession-_gerd_altman_pixaby.jpg?width=1200&height=800&fit=crop)\n",
      "\n",
      "3. **Title:** [Rare Stock Picks In September 2023 - From 32 Discerning Analysts](https://seekingalpha.com/article/4639228-draft-rare-stock-picks-in-september-2023-from-32-discerning-analysts)  \n",
      "   **Published on:** October 6, 2023  \n",
      "   **Description:** This article highlights investment picks from analysts, including META among other stocks.  \n",
      "   ![Image](https://static.seekingalpha.com/uploads/2023/9/2/saupload_haystack.png)\n",
      "\n",
      "4. **Title:** [Blowout Jobs Report But Expect Momo Gurus To Try To Run Up The Market On Hourly Earnings](https://www.benzinga.com/markets/23/10/35127411/blowout-jobs-report-but-expect-momo-gurus-to-try-to-run-up-the-market-on-hourly-earnings)  \n",
      "   **Author:** The Arora Report  \n",
      "   **Published on:** October 6, 2023  \n",
      "   **Description:** The article discusses the strong jobs report and its implications for the market, mentioning META among other major stocks.  \n",
      "   ![Image](https://cdn.benzinga.com/files/totos-adam-3r8rcsy0ffg-unsplash_1.jpg?width=1200&height=800&fit=crop)\n",
      "\n",
      "5. **Title:** [Are You a Momentum Investor? This 1 Stock Could Be the Perfect Pick](https://www.zacks.com/stock/news/2161565/are-you-a-momentum-investor-this-1-stock-could-be-the-perfect-pick)  \n",
      "   **Author:** Zacks Equity Research  \n",
      "   **Published on:** October 6, 2023  \n",
      "   **Description:** This article discusses momentum investing and highlights META as a potential pick based on Zacks Style Scores.  \n",
      "   ![Image](https://staticx-tuner.zacks.com/images/default_article_images/default25.jpg)\n",
      "\n",
      "For more detailed information, you can click on the article titles to read the full stories.\n"
     ]
    }
   ],
   "source": [
    "print(dappier_tool.search_stock_market_data(\"latest financial news on Meta\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d41fb0d3",
   "metadata": {},
   "source": [
    "### Using the Dappier Real Time Search tool in an Agent\n",
    "\n",
    "We can create an agent with access to the Dappier Real Time Search tool start testing it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a33051ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "agent = FunctionAgent(\n",
    "    tools=dappier_tool_list,\n",
    "    llm=OpenAI(model=\"gpt-4o\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba290fe8f1621839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here's a daily clothing recommendation for next week in New York based on the weather forecast:\n",
      "\n",
      "### Monday\n",
      "- **Weather:** Partly cloudy, high around 45°F (7°C).\n",
      "- **Clothing Recommendation:** Wear a warm coat, a scarf, and gloves. Layer with a sweater underneath.\n",
      "\n",
      "### Tuesday\n",
      "- **Weather:** Mostly sunny, high near 48°F (9°C).\n",
      "- **Clothing Recommendation:** A light jacket with a long-sleeve shirt should be comfortable. Sunglasses might be a good idea.\n",
      "\n",
      "### Wednesday\n",
      "- **Weather:** Cloudy with a chance of rain, high about 50°F (10°C).\n",
      "- **Clothing Recommendation:** Wear a waterproof jacket or a raincoat. Don't forget an umbrella!\n",
      "\n",
      "### Thursday\n",
      "- **Weather:** Rain likely, high around 52°F (11°C).\n",
      "- **Clothing Recommendation:** A waterproof coat and waterproof shoes are essential. Consider wearing a hat to keep dry.\n",
      "\n",
      "### Friday\n",
      "- **Weather:** Mostly cloudy, high near 49°F (9°C).\n",
      "- **Clothing Recommendation:** A medium-weight jacket with a sweater underneath. A scarf can add extra warmth.\n",
      "\n",
      "### Saturday\n",
      "- **Weather:** Partly sunny, high about 46°F (8°C).\n",
      "- **Clothing Recommendation:** A warm coat and a beanie. Layering is key to staying comfortable.\n",
      "\n",
      "### Sunday\n",
      "- **Weather:** A bit warmer, high around 50°F (10°C).\n",
      "- **Clothing Recommendation:** A light jacket with a cozy sweater. A scarf can be a stylish addition.\n",
      "\n",
      "Stay warm and enjoy the week!\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    await agent.run(\n",
    "        \"Analyze next week's weather in New York and provide daily clothing recommendations in a markdown format.\"\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "709c6a64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In the last 24 hours, Apple (AAPL) stock has seen the following activity:\n",
      "\n",
      "- **Price Range:** $173.26 to $173.30\n",
      "- **Notable Trades:**\n",
      "  - **$173.27** for 86 shares on Exchange 11\n",
      "  - **$173.27** for 10 shares on Exchange 11\n",
      "  - **$173.28** for 1 share on Exchange 11\n",
      "  - **$173.28** for 3 shares on Exchange 11\n",
      "  - **$173.27** for 1 share on Exchange 4\n",
      "  - **$173.2701** for 1 share on Exchange 4\n",
      "  - **$173.27** for 112 shares on Exchange 11\n",
      "  - **$173.29** for 10 shares on Exchange 11\n",
      "  - **$173.29** for 1 share on Exchange 12\n",
      "  - **$173.26** for 91 shares on Exchange 12\n",
      "\n",
      "The stock has shown minor fluctuations within this range. If you need further details or analysis, feel free to ask!\n"
     ]
    }
   ],
   "source": [
    "print(await agent.run(\"Last 24-hour activity of apple stock\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama-index-yhiCGw8g-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
